/**
 * Created by Administrator on 2016/2/29.
 */

//兑换券比赛入口
gpNamespace.DhqLayer = cc.Layer.extend({
	gameDetailPic:null,
	background:null,
    levelMenu:null,
	startItem:null,
    selectedKuang:null,
    isApplying:false,//是否正在报名
    gameLevel:1,
    toplayer:null,
    levalTicketArray:[2000,5000,10000,100000],

    ctor: function () {
        this._super();
        this.init();
    },
    init: function () {
    	cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkdhqRes.Pdkdhq_plist);
    	ccs.armatureDataManager.addArmatureFileInfo(
    			gpNamespace.dhqStartAnimationRes.DhqStart_png,
    			gpNamespace.dhqStartAnimationRes.DhqStart_plist,
    			gpNamespace.dhqStartAnimationRes.DhqStart_json);
/*    	ccs.armatureDataManager.addArmatureFileInfo(
    			gpNamespace.dhqWaitingAnimationRes.DhqWaiting_png,
    			gpNamespace.dhqWaitingAnimationRes.DhqWaiting_plist,
    			gpNamespace.dhqWaitingAnimationRes.DhqWaiting_json);*/
        var size = cc.winSize;

        //背景图层
        var background = new cc.Sprite(gpNamespace.dhq.Dhq_Bg);
        background.attr({
            x: size.width / 2,
            y: size.height / 2+30,
            anchorX: 0.5,
            anchorY: 0.5
        });
        this.background = background;
        this.addChild(background);
        
        //比赛详情
        var gameDetail = new cc.MenuItemSprite(
        		new cc.Sprite(gpNamespace.dhq.Dhq_Detail),
        		new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_Detail),
        		this.gameDetailCallback, this);
        gameDetail.attr({
        	x:150,
        	y:510
        });
        var detailMenu = new cc.Menu(gameDetail);
        detailMenu.attr({
        	x: 0,
        	y: 0,
        	anchorX: 0,
        	anchorY: 0
        });
        background.addChild(detailMenu,1);
        //等待时间
/*        var waitingDate = new cc.Sprite(gpNamespace.dhq.Dhq_WaitingDate);
        waitingDate.attr({
        	x:300 + waitingDate.width/2,
        	y:510
        });
        background.addChild(waitingDate,1);*/
        
        /*4个赛场图片*/
        var dhq2000M = new cc.MenuItemSprite(
            new cc.Sprite(gpNamespace.dhq.Dhq_2000M),
            new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_2000M),
            this.selectLeval, this);
        dhq2000M.setPosition(-420, 0);
        dhq2000M.setUserData(1);

        var dhq5000M = new cc.MenuItemSprite(
            new cc.Sprite(gpNamespace.dhq.Dhq_5000M),
            new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_5000M),
            this.selectLeval, this);
        dhq5000M.setPosition(-140, 0);
        dhq5000M.setUserData(2);

        var dhq10000M = new cc.MenuItemSprite(
            new cc.Sprite(gpNamespace.dhq.Dhq_10000M),
            new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_10000M),
            this.selectLeval, this);
        dhq10000M.setPosition(140, 0);
        dhq10000M.setUserData(3);

        var dhq100000M = new cc.MenuItemSprite(
            new cc.Sprite(gpNamespace.dhq.Dhq_100000M),
            new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_100000M),
            this.selectLeval, this);
        dhq100000M.setPosition(420, 0);
        dhq100000M.setUserData(4);

        var levelMenu = new cc.Menu(dhq2000M, dhq5000M, dhq10000M,dhq100000M);
        levelMenu.attr({
            x: 580,
            y: 280
        });
        this.levelMenu = levelMenu;
        background.addChild(levelMenu,1);

        var selectedKuang = new cc.Sprite(gpNamespace.dhq.Dhq_Selected);
        selectedKuang.attr({
            x: 160,
            y: 280
        });
        background.addChild(selectedKuang);
        this.selectedKuang = selectedKuang;

        //快速报名
        var baoMingBt = new cc.MenuItemSprite(
            new cc.Sprite(gpNamespace.dhq.Dhq_Apply),
            new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_Apply),
            this.baoMing, this);
        baoMingBt.attr({
            x: 580,
            y: 20,
        });
        this.baoMingBt = baoMingBt;

        //取消报名
        var baoMingCancelBt = new cc.MenuItemSprite(
            new cc.Sprite(gpNamespace.dhq.Dhq_Cancel),
            new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_Cancel),
            this.baoMingCancel, this);
        baoMingCancelBt.attr({
            x: 580,
            y: 20,
        });
        baoMingCancelBt.setVisible(false);
        this.baoMingCancelBt = baoMingCancelBt;

        var startMenu = new cc.Menu(baoMingBt,baoMingCancelBt);
        startMenu.x = 0;
        startMenu.y = 0;
        this.startMenu = startMenu;
        background.addChild(startMenu,10);

        //动画
        var startAvi = new ccs.Armature("dh_cdd_kuaisubaoming_dh");
        startAvi.getAnimation().playWithIndex(0);
        startAvi.getAnimation().setSpeedScale(1);
        startAvi.setScale(1);
        startAvi.setAnchorPoint(0, 0);
        startAvi.setPosition(-5, -5);
        baoMingBt.addChild(startAvi,3);
        startAvi.getAnimation().setMovementEventCallFunc(this.freeStratCallBackRemove, this);
        
        //比赛详情pic
        var gameDetailPic = new cc.Sprite(gpNamespace.dhq.Dhq_DetailPic);
        gameDetailPic.attr({
        	x:size.width/2,
        	y:size.height/2,
        });
        gameDetailPic.visible = false;
        this.gameDetailPic = gameDetailPic;
        this.addChild(gameDetailPic,5);
        
        //返回按钮
        var backBt = new cc.MenuItemSprite(
    		new cc.Sprite(gpNamespace.dhq.Dhq_Detail_Back),
    		new tyNamespace.maskSprite(gpNamespace.dhq.Dhq_Detail_Back),
    		this.backBtCallback, this);
        backBt.attr({
        	x:580,
        	y:70,
        });
        var backMenu = new cc.Menu(backBt);
        backMenu.x = 0;
        backMenu.y = 0;
        gameDetailPic.addChild(backMenu,6);

    },

    freeStratCallBackRemove: function (armature, movementType, movementID) {
    	if (movementType == ccs.MovementEventType.complete) {
    		armature.removeFromParent(true);
		}
	},

    backBtCallback:function(){
    	this.gameDetailPic.visible = false;
    	this.background.visible = true;
        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonBack, false);//返回音效
    },
    
    gameDetailCallback:function(){
    	this.gameDetailPic.visible = true;
    	this.background.visible = false;
        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonBack, false);
    },

    selectLeval:function(sender) {
        this.gameLevel = sender.getUserData();
        this.selectedKuang.setPositionX(160 + 280 * (this.gameLevel-1));
        cc.log("游戏等级:"+this.gameLevel);
    },

    baoMing:function(sender){
        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
        tyNamespace.setMenuEnable(sender,1);
        cc.log("Start is clicked!GameLevel:"+this.gameLevel);

        if(userInfo.gold < this.levalTicketArray[this.gameLevel-1]){
            tyNamespace.warningJumper(1, tyNamespace.WarningSectencesIndex.NoGold);
            return;
        }

        this.isApplying = true;
        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

        var dzmjNet = gpNamespace.NetManager.getInstance();
        dzmjNet.baoMingLayer = this;

        this.baoMingBt.setVisible(false);
        this.baoMingCancelBt.setVisible(true);
        this.levelMenu.setEnabled(false);
        this.levelMenu.setOpacity(200);

        //等待中
        var armature = new ccs.Armature("loading_001u");
        armature.getAnimation().playWithIndex(0);
        armature.getAnimation().setSpeedScale(1);
        armature.setScale(1);
        armature.setAnchorPoint(0.5, 0.5);
        armature.setPosition(640, 360);
        armature.setTag(11);
        this.addChild(armature,10);

        var cddNet = gpNamespace.NetManager.getInstance();
        cddNet.baoMingLayer = this;
        xinyou.netManager.inRoom(6, this.gameLevel+21, 1);
        gpNamespace.CurrentDhqLevel = this.gameLevel;
        this.toplayer = cc.director.getRunningScene().getChildByTag(101);
        userInfo.gold = userInfo.gold - this.levalTicketArray[this.gameLevel-1];
        this.toplayer && this.toplayer.setUserGold(userInfo.gold);
    },
    baoMingCancel:function(sender){
        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonBack, false);
        tyNamespace.setMenuEnable(sender,1);
        cc.log("取消报名");
        this.isApplying = false;
        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonBack, false);

        this.baoMingBt.setVisible(true);
        this.baoMingCancelBt.setVisible(false);
        this.levelMenu.setEnabled(true);
        this.levelMenu.setOpacity(255);
        //移除waiting
        var waitingLayer = this.getChildByTag(11);
        waitingLayer && waitingLayer.removeFromParent(true);

        userInfo.gold = userInfo.gold + this.levalTicketArray[this.gameLevel-1];
        this.toplayer && this.toplayer.setUserGold(userInfo.gold);

        currentNetManager.leaveRoom();
    },
    onExit:function(){
        this._super();
        if(this.isApplying){
            tyNamespace.fadeInThenOut("已取消报名兑换券赛");
            userInfo.gold = userInfo.gold + this.levalTicketArray[this.gameLevel-1];
            this.toplayer && this.toplayer.setUserGold(userInfo.gold);
            currentNetManager && currentNetManager.leaveRoom();
        }
    }
});